No.
All you need is money OR credit. Just one would do. Of course, if you had lots of money and plenty of credit you could certainly buy the car.
Sometimes a program has to test if just one of the conditions have been met. Here is how that is done with the car purchase problem:
// Sports Car Purchase
//
// You need $25,000 in cash or credit
// for a hot red Miata sports car.
//
import java.io.*;
class HotWheels
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin =
new BufferedReader ( new InputStreamReader( System.in ) );
String inData;
int cash, credit ;
// get the cash
System.out.println("How much cash?");
inData = stdin.readLine();
cash = Integer.parseInt( inData );
// get the credit line
System.out.println("How much credit?");
inData = stdin.readLine();
credit = Integer.parseInt( inData );
// check that at least one qualification is met
if ( cash >= 25000 || credit >= 25000 )
System.out.println("Enough to buy this car!" );
else
System.out.println("What about a Yugo?" );
}
}
The symbol ||
(vertical-bar vertical-bar)
means "OR."
On your keyboard, it is the top character on the key above the "enter" key.
It evaluates to true when either qualification is met
or when both are met.
The if
statement asks a question with two parts:
if cash >= 25000 || credit >= 25000
---------- ----------
cash part credit part
Each one of these parts is a relational expression (just as with previous programs in this chapter.) A relational expression computes true or false from two numbers. A logical expression computes a single true or false from two or more true/false values.